#!/usr/bin/env python


# essa classe implementa as regras do jogo
# uma versao alternativa do jogo pode ser feita
# fazendo alternativa a ela
class Rules():
    """Rules implementa as regras originais do Uno
    """
    def valid_move(self, game, play):
        top_card = game.top_card
        if play.player != game.cur_player:
            return False
	if play.card == None:	# carta nula eh pedido de compra
	    return True
        if top_card.color == BLACK:
            if play.card.color == game.chosen_color:
                return True
        if play.card.match_color_or_number(top_card):
            return True
        return False
    def apply_power(self, game, power):
	if power == SKIP:
	    game.next_player()
	elif power == REVERSE:
	    game.players.reverse()
	elif power == BUYTWO:
	    game.next_player()
	    game.cur_player.add_cards(game.deck.deal_cards(2))
	elif power == BUYFOUR:
	    game.next_player()
	    game.cur_player.add_cards(game.deck.deal_cards(4))
    def apply(self, game, play):
        if self.valid_move(game, play):
            # se for jogada valida, aplica alguma regra especial,
            # e passa a vez para o proximo jogador
            if len(play.player.cards) == 0:
                print str(play.player.name) + ' venceu!'
                return
	    if play.card == None:
		play.card = game.deck.deal()
		if not self.valid_move(game, play):
		    play.player.add_card(play.card)
		    return
            if len(play.player.cards) == 1:
                print str(play.player.name) + ' UNOed!'
	    if play.card.has_power():
		self.apply_power(game, play.card.number)
            if play.card.is_wild():
                game.chosen_color=play['chosen_color']
	    game.top_card=play.card
            game.next_player()
        else:
            # penalidade pra quem jogar errado eh comprar duas
            play.player.add_cards(game.deck.deal_cards(2))
            print play.player.name + ' tentou jogar ' + str(play.card)
